<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="group9.User" %>
<%@ page import="group9.UserStory" %>
<%@ page import="group9.PMF" %>
<%@ page import="java.util.LinkedList" %>
<%@ page import="java.util.List" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.google.appengine.api.datastore.Key" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

	<!-- Claudio Herreros, Dylan James, Tyler Berg -->

	<% 
		User user = (User)session.getAttribute("User");

		if (user == null)
		{
			response.sendRedirect("login.jsp");
		}
	
		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.getFetchPlan().addGroup("all");
		user = (User) pm.getObjectById(User.class, user.getKey());

		if (user == null)
		{
			response.sendRedirect("index.html");
			return;
		}
	

		session.setAttribute("User", user);
	
		LinkedList<UserStory> pending = new LinkedList<UserStory>();
		LinkedList<UserStory> estimated = new LinkedList<UserStory>();
		LinkedList<UserStory> needed = new LinkedList<UserStory>();
	
		List<UserStory> stories = user.getMyUserStories();
		
		List<Key> estim = user.getEstUserStories();
		
		String query = "select from " + UserStory.class.getName();
		List<UserStory> allStories = (List<UserStory>) pm.newQuery(query).execute();
		
		for(int i = 0; i < allStories.size(); i++){
			if(allStories.get(i).existsEstimator(user)){
				needed.add(allStories.get(i));
			}
		}
		
	
		if (stories != null)
		{
			for (UserStory us : user.getMyUserStories())
			{
				us.getEstimates();
				if (us.pending())
				{
					pending.add(us);
				}else{
					estimated.add(us);
				}
			}
		}else{
			response.sendRedirect("NULL");
		}
	%>
	<head>
		<link rel="stylesheet" type="text/css" href="styles.css" title="Style 1" />
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title><%= user.getUserName() %>'s User Stories</title>
	</head>
	<body>
		<h1><%= user.getFullName() %>'s User Stories</h1>
		<a href="/SignOut" class="Signout">SignOut</a></span>
		<br/>
		<hr />

		<h2>Pending:</h2>
		<% 
			session.setAttribute("pending", pending);
			if (pending.isEmpty())
			{
				%>
				<div align="center" class="Info">You have no pending User Stories.</div>
				<%
			}else{
				%>
				<ul>
				<%
					int counter = 0;
					for (UserStory us : pending)
					{					
					%>
						<li>
							<a href="ViewUserStory.jsp?type=p&index=<%=counter%>">
							<%= us.getTitle() %></a>
						</li>
						<br />
						<ul><li>
							<b>Description:</b> <br/><%= us.getDescription() %>
						</li></ul>
						<br />
					<%
					counter++;
					}
				%>
				</ul>
				<%
			}
			%>
			<h2>Estimated:</h2>
			<% 
			if (estimated.isEmpty())
			{
				%>
				<div align="center" class="Info">You have no estimated User Stories.</div>
				<%
			}else{
				session.setAttribute("estimated", estimated);
				%>
				<table align="center"  border="1" cellspacing="0" cellpadding="5">
					<tr>
						<td>
							<span align="center"><strong>Title</strong></span>
						</td>
						<td>
							<span align="center"><strong>Estimate</strong></span>
						</td>
					</tr>
					<%
					int total = 0;
					int counter = 0;
					for (UserStory us : estimated)
					{
						%>
						<tr>
							<td>
								<a href="ViewUserStory.jsp?type=e&index=<%=counter%>">
									<span class="FieldLabel"><%= us.getTitle() %></span>
								</a>
							</td>
							<td>
								<span class="Field"><%= us.getEstimate() %></span>
							</td>
							<%
							total += us.getEstimate();
							counter++;
					}
					%>
					</tr>
					<tr>
						<td>
							<span class="FieldLabel"><strong>Total:</strong></span>
						</td>
						<td>
							<span class="Field"><strong><%=  total %></strong></span>
						</td>
					</tr>
				</table>
				<%
			}
			%>
			<h2>Estimation Needed:</h2>
			<% 
				session.setAttribute("needed", needed);
				if (needed.isEmpty())
				{
					%>
					<div align="center" class="Info">There are no UserStories you need to estimate.</div>
					<%
				}else{
					%>
					<ul>
					<%
						int counter = 0;
						for (UserStory us : needed)
						{					
						%>
							<li>
								<a href="EstimateUserStory.jsp">
								<%= us.getTitle() %></a>
							</li>
							<br />
							<ul><li>
								<b>Description:</b> <br/><%= us.getDescription() %>
							</li></ul>
							<br />
						<%
						counter++;
						}
					%>
					</ul>
					<%
				}
			pm.close();
			%>
		<br/>
		<hr/>
		<a href="home.jsp">Back!</a>
	</body>
</html>
